package repeat.personal.util;


import org.apache.commons.lang.StringUtils;
import repeat.personal.exception.CompanyNameException;

import javax.servlet.http.HttpServletRequest;

/**
 * 校验断言（抛出检验异常 CompanyNameException）
 * @author  Zhanggq
 * @date    2019年6月18日 上午9:52:58
 * @version
 */
public class VerifyAssert {
	
	/**
	 * 判断一个对象是否为空，是则抛出异常
	 * 
	 * @param obj
	 * @throws CompanyNameException
	 */
	public static void isNotNull(Object obj) throws CompanyNameException {
		if(obj == null){
			throw new CompanyNameException("数据不能为空");
		}else if(org.apache.commons.lang.StringUtils.isBlank(obj.toString())){
			throw new CompanyNameException("数据不能为空");
		}
	}
	
	/**
	 * 判断一个对象是否为空，是则抛出异常
	 * 
	 * @param obj
	 * @param errorMsg 错误信息
	 * @throws CompanyNameException
	 */
	public static void isNotNull(Object obj, String errorMsg) throws CompanyNameException {
		if(obj == null){
			throw new CompanyNameException(errorMsg);
		}else if(org.apache.commons.lang.StringUtils.isBlank(obj.toString())){
			throw new CompanyNameException(errorMsg);
		}
	}
	
	/**
	 * 判断一个值是否为true，不是则抛出校验异常
	 * @param bl
	 * @param errorMsg 错误信息
	 * @throws CompanyNameException
	 */
	public static void isTrue(boolean bl, String errorMsg) throws CompanyNameException {
		if(bl != true){
			throw new CompanyNameException(errorMsg);
		}
	}
	
	/**
	 * 获取整形参数(空和格式不对则抛校验异常)
	 * 
	 * @param request
	 * @param param
	 * @return
	 * @throws CompanyNameException
	 */
	public static Integer getParamToInt(HttpServletRequest request, String param) throws CompanyNameException {
		String parameter = request.getParameter(param);
		if(StringUtils.isBlank(parameter)){
			throw new CompanyNameException("参数：" + param + "不能为空");
		}
		
		int result = 0;
		try {
			result = Integer.parseInt(parameter);
		} catch (Exception e) {
			throw new CompanyNameException("参数：" + param + "格式有误");
		}
		
		return result;
	}
	
	/**
	 * 获取字符串参数(空则抛校验异常)
	 * 
	 * @param request
	 * @param param
	 * @return
	 * @throws CompanyNameException
	 */
	public static String getParamToString(HttpServletRequest request, String param) throws CompanyNameException {
		String parameter = request.getParameter(param);
		if(org.apache.commons.lang.StringUtils.isBlank(parameter)){
			throw new CompanyNameException("参数：" + param + "不能为空");
		}
		
		return parameter;
	}
	
}

